Yacon.data.FieldList = function()
{
	this.fieldNames    =  new Array();
	this.fieldList     =  new Array();
	this.fjsonList      =  null;
}
Yacon.data.FieldList.Extend(Object,{
	
	setJsonList:function(jsonList)
	{
		this.fjsonList = jsonList;
	},
	
	addField:function(field)
	{
		if (Yacon.isEmpty(field)||Yacon.isEmpty(field.getFieldName()))
			return;
		
		var index = this.fieldNames.indexOf(field.getFieldName().toLowerCase());
	    if (index >-1)
		{
			field.setFieldList(this);
			field.setFieldIndex(index);
			this.fieldList[index] = field;
		}else
		{
			var fieldcount = this.fieldList.length;
			field.setFieldList(this);
			field.setFieldIndex(fieldcount);
			this.fieldNames[fieldcount] = field.getFieldName().toLowerCase();
			this.fieldList[fieldcount] = field;
		}
	  
	},
	getField:function(fieldIndex)
	{
		var field = null;
	    if (typeof(fieldIndex) == "number")
		{
	     	field = this.fieldList[fieldIndex];
	    }else  if (typeof(fieldIndex) == "string")
		{
	    	var index = this.fieldNames.indexOf(fieldIndex.toLowerCase());
		    if (index >-1)
			{
				field= this.fieldList[index];
			}
		}
		return field;
	},
	getJsonField:function(fieldIndex)
	{
		var field = null;
	    if (typeof(fieldIndex) == "string" && this.fjsonList!= null)
		{
	    	field = this.fjsonList[fieldIndex.toLowerCase()];
	    }
		return field;
	},
	getFieldCount:function()
	{
		return this.fieldList.length;
	},
	setDataSet:function(dataSet)
	{
		this.fDataSet = dataSet;
	},
	getDataSet:function()
	{
    	if (this.fDataSet)
    	{
    		return this.fDataSet;
    	}else
    	{
    		return null;
    	}
	},
	free:function()
	{
		this.fieldNames.clear();
		this.fieldList.clear();
		this.fjsonList  = null;
		
	}
});

Yacon.data.Record = function()
{
	this.fieldNames    =  new Array();
	this.fieldList     =  new Array();
	this.fjsonList     =  null;
	this.ftype         = "record";
	this.fState        = "past"; 
}

Yacon.data.Record.Extend(Object,{
	
	setJsonList:function(jsonList)
	{
		this.fjsonList = jsonList;
	},
	
	addField:function(field)
	{
		if (Yacon.isEmpty(field)||Yacon.isEmpty(field.getFieldName()))
			return;
		
		var index = this.fieldNames.indexOf(field.getFieldName().toLowerCase());
		
	    if (index > -1)
		{
			field.setRecord(this);
			field.setFieldIndex(index);
			this.fieldList[index] = field;
		}else
		{
			var fieldcount = this.getFieldCount();			
			field.setRecord(this);
			field.setFieldIndex(fieldcount);
			this.fieldNames[fieldcount] = field.getFieldName().toLowerCase();
			this.fieldList[fieldcount] = field;
			
		}
	  
	},
	
	getField:function(fieldIndex)
	{
		var field = null;
	    if (typeof(fieldIndex) == "number")
		{
	     	field = this.fieldList[fieldIndex];
	    }else  if (typeof(fieldIndex) == "string")
		{
	    	var index = this.fieldNames.indexOf(fieldIndex.toLowerCase());
	    	if (index >-1)
			{
		    	field= this.fieldList[index];
			}
		}
		return field;
	},
	isExitField:function(fieldIndex)
	{
		var isexit = false;
		if (Yacon.isString(fieldIndex))
	    {
			var index = this.fieldNames.indexOf(fieldIndex.toLowerCase());
			if (index>=0 && index < this.getFieldCount())
	    		  isexit = true;
	    }
		return isexit;	
	},
	getJsonField:function(fieldIndex)
	{
		var field = null;
	    if (typeof(fieldIndex) == "string" && this.fjsonList!= null)
		{
	    	field = this.fjsonList[fieldIndex.toLowerCase()];
	    }
		return field;
	},
	getFieldValue:function(fieldIndex)
	{
		var valueStr = ""; 
		var field = this.getField(fieldIndex);		 
		
		if (field != null)
		 {
			 valueStr = field.getFieldValue();
		 }else
		 {
			 var field = this.getJsonField(fieldIndex);
			 if (field != null)
			 {
				 valueStr = field.value;
			 }			 
		 }
		 return valueStr;
		 
	},
	setFieldValue:function(fieldIndex,fieldValue,fieldText)
	{
	 	
		var field = this.getField(fieldIndex);
	  	if (field == null)
	 	{
	 		field = new Yacon.data.FieldValue();
	 	
	 		field.setFieldName(fieldIndex);	
	 		this.addField(field);	
	 	}
	 	
	 	field.setFieldValue(fieldValue);
	 	if (fieldText)
	 		field.setFieldText(fieldText);
	 	
	 	if (this.fState == "past")
	 	{
	 		field.setFieldOldValue(fieldValue);
	 		if (fieldText)
		 		field.setFieldOldText(fieldText);
	 		
	 	}else if (this.fState == "edit")
	 	{
	 		 var jsonField = this.getJsonField(fieldIndex);
			 if (jsonField != null)
			 {
				 field.setFieldOldValue(jsonField.value);
				 field.setFieldOldText(jsonField.text);
			 }
		}	 		
	 	return field;
	 },	
	 getFieldOldValue:function(fieldIndex)
	 {
	 	var valueStr = ""; 
		var field = this.getField(fieldIndex);		 
		if (field != null)
		 {
			 valueStr = field.getFieldOldValue();
		 }else
		 {
			 var field = this.getJsonField(fieldIndex);
			 if (field != null)
			 {
				 valueStr = field.value;
			 }			 
		 }
		 return valueStr;
	  },
	  setFieldOldValue:function(fieldIndex,fieldOldValue)
	  {
		  var field = this.getField(fieldIndex);
		  if (field == null)
		 	  return null;
		  
		  field.setFieldOldValue(fieldOldValue);
		  return field;
	 },
	 getFieldText:function(fieldIndex)
	 {
		 var valueStr = ""; 
		 var field = this.getField(fieldIndex);		 
		 if (field != null)
		 {
			 valueStr = field.getFieldText();
		 }else
		 {
			 var field = this.getJsonField(fieldIndex);
			 if (field != null)
			 {
				 valueStr = field.text;
			 }			 
		 }
		 return valueStr;
	  },
	 setFieldText:function(fieldIndex,fieldText)
	 {
		var field = this.getField(fieldIndex);
		if (field == null)
		{
			return null;
		}	
		
		field.setFieldText(fieldText);
		
		if (this.fState == "past")
			field.setFieldOldText(fieldText);
		
		return field;
	},
	setState:function(state)
	{
		switch (state.toLowerCase())
		{
			case "insert":
			case "edit":
			case "delete":
			case "select":
			{
				this.fState =state.toLowerCase();
				break;
			}
			default :
			{
				this.fState = "past";
			    break;	
			}
		}
	},
	 getState:function()
	 {
		 return this.fState;
	 },
	 
	 fieldRevive:function()
	 {
	 	if (this.getState() == "edit")
	 	{
	 		for(var i = 0; i< this.getFieldCount();i++)
	 		{
	 			this.setFieldValue(i,this.getField(i).getFieldOldValue());
	 			this.setFieldText(i,this.getField(i).getFieldOldText());
	 		}
	 	}
	 },
	 
	 jsonTransform:function()
	 {
		 //从JSON转换内部定义字段
	 },
	 
	 getFieldCount:function()
	 {
		return this.fieldList.length;
	 },
	 preRecord:function()
	 {
		 return this.prevElement;
	 },
	 nextRecord:function()
	 {
		 return this.nextElement;
	 },
 	 setDataSet:function(dataSet)
	 {
		this.fDataSet = dataSet;
	 }, 
	 getDataSet:function()
	 {
    	if (this.fDataSet)
    	{
    		return this.fDataSet;
    	}else
    	{
    		return null;
    	}
	 },
 	 setChildDataSet:function(childDataSet)
	 {
		 this.data = childDataSet;
		 if (this.data != null)
		 {
			this.data.parentRecord  	 = this;		
			this.data.findRecord    	 = this.fDataSet.findRecord;
			this.data.attributes    	 = this.fDataSet.attributes;
			this.data.excuteMethod       = this.fDataSet.excuteMethod; 
			this.data.refreshMethod      = this.fDataSet.refreshMethod; 
			this.data.afterRefreshMethod = this.fDataSet.afterRefreshMethod; 
			this.data.afterExcuteMethod  = this.fDataSet.afterExcuteMethod; 
			this.data.parentDataset      = this.fDataSet;
			
		 }
	 },	 
	 getChildDataSet:function()
	 {
	 	 if(this.data)
		 {
			return this.data;
		 }else
		 {
			 return this.createChildDataSet();
		 }
	 },
	 createChildDataSet:function()
	 {
		 this.data = new Yacon.data.DataSet();
		 this.setChildDataSet(this.data);
		 return this.data;
	 },
     free:function()
	 {
		  if (this.data && this.data.free)
		  {
			  this.data.free();
			  this.data = null;
		  }
		  
		  this.fieldNames.clear();
		  this.fieldList.clear();
		
		  this.fjsonList  = null;
	 }
});


